package com.alter.utils.web;

import net.htmlparser.jericho.Source;

import org.springframework.util.Assert;

/**
 * HTML 解析
 * 
 * @author
 */
public final class HTMLUtil {
    
    /**
     * 按字节截取context
     * 
     * @param context
     *            截取对象
     * @param articleBriefSize
     *            字节大小
     * @return
     */
    public static String createBrief(String context, int articleBriefSize) {
	Assert.isTrue(articleBriefSize > 0, "Size 必须大于  0!");
	Source source = new Source(context);
//	String text = source.getRenderer().setIncludeHyperlinkURLs(false).setIncludeAlternateText(false).setIncludeFirstElementTopMargin(false).toString();
	String text = source.getTextExtractor().setExcludeNonHTMLElements(true).setIncludeAttributes(true).setConvertNonBreakingSpaces(true).toString();
	if (text.length() > articleBriefSize) {
	    return text.substring(0, articleBriefSize - 1) + "...";
	}
	return text;
    }
    
}
